package sample.ui

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import zx.tas.model.GABPanelPlanV2
import com.netease.core.R
import kotlinx.coroutines.script.C
import kotlinx.coroutines.script.tes.colorInt
import kotlinx.coroutines.script.tes.printLog
import kotlinx.coroutines.script.tes.toast
import kotlin.math.max

class GABPanelV2Layout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
    private var data: GABPanelPlanV2? = null
    private var selectedKey: String? = null
    init {
        LayoutInflater.from(context).inflate(R.layout.sk, this)
        if (isInEditMode) {
            // do nothing
        } else {
            findViewById<View>(R.id.atk).setOnClickListener { select(C.V_ATK) }
            findViewById<View>(R.id.skillA).setOnClickListener { select(C.V_SKILL_A) }
            findViewById<View>(R.id.skillB).setOnClickListener { select(C.V_SKILL_B) }
            findViewById<View>(R.id.skillC).setOnClickListener { select(C.V_SKILL_C) }
            findViewById<View>(R.id.skillD).setOnClickListener { select(C.V_SKILL_D) }
            findViewById<View>(R.id.resetDefault).setOnClickListener {
                data = GABPanelPlanV2.default
                toast("随机降低频率释放技能.")
                update()
                save()
            }
            findViewById<View>(R.id.resetFast).setOnClickListener {
                data = GABPanelPlanV2.fast
                toast("技能能使用了就直接释放.不等待")
                update()
                save()
            }
        }
    }

    fun setup() {
        data = Properties.gabPanelPlan
        selectedKey = C.V_ATK
        update()
    }

    private fun select(value: String) {
        printLog { "GAB select $value" }
        selectedKey = value
        update()
    }

    private fun update() {
        val key = selectedKey
        if (key.isNullOrEmpty()) return
        val value = data ?: return
        val selectSkill = value.requireItem(key)

        findViewById<TextView>(R.id.type).text = key
        findViewById<TextView>(R.id.atkText).setTextColor(if (key == C.V_ATK) 0xffffd700.toInt() else Color.WHITE)
        findViewById<TextView>(R.id.skillAText).setTextColor(if (key == C.V_SKILL_A) 0xffffd700.toInt() else Color.WHITE)
        findViewById<TextView>(R.id.skillBText).setTextColor(if (key == C.V_SKILL_B) 0xffffd700.toInt() else Color.WHITE)
        findViewById<TextView>(R.id.skillCText).setTextColor(if (key == C.V_SKILL_C) 0xffffd700.toInt() else Color.WHITE)
        findViewById<TextView>(R.id.skillDText).setTextColor(if (key == C.V_SKILL_D) 0xffffd700.toInt() else Color.WHITE)

        findViewById<TextView>(R.id.atkValue).also {
            it.text = value.atk.infoText
            it.setTextColor(if (value.atk.enable == 0) "#FF0000".colorInt else "#50C878".colorInt)
        }
        findViewById<TextView>(R.id.skillAValue).also {
            it.text = value.skillA.infoText
            it.setTextColor(if (value.skillA.enable == 0) "#FF0000".colorInt else "#50C878".colorInt)
        }
        findViewById<TextView>(R.id.skillBValue).also {
            it.text = value.skillB.infoText
            it.setTextColor(if (value.skillB.enable == 0) "#FF0000".colorInt else "#50C878".colorInt)
        }
        findViewById<TextView>(R.id.skillCValue).also {
            it.text = value.skillC.infoText
            it.setTextColor(if (value.skillC.enable == 0) "#FF0000".colorInt else "#50C878".colorInt)
        }
        findViewById<TextView>(R.id.skillDValue).also {
            it.text = value.skillD.infoText
            it.setTextColor(if (value.skillD.enable == 0) "#FF0000".colorInt else "#50C878".colorInt)
        }
        findViewById<TextView>(R.id.skillDValue).also {
            it.text = value.skillD.infoText
            it.setTextColor(if (value.skillD.enable == 0) "#FF0000".colorInt else "#50C878".colorInt)
        }
        findViewById<TextView>(R.id.skillEnable).also {
            it.text = if (selectSkill.enable == 0) "启用" else "禁用"
            it.setOnClickListener {
                val last = data ?: return@setOnClickListener
                val enable = if (selectSkill.enable == 0) 1 else 0
                data = last.updateItem(key, selectSkill.copy(enable = enable))
                update()
                save()
            }
        }
        findViewById<TextView>(R.id.skillEnableType).also {
            it.visibility = if (selectSkill.enable != 0) VISIBLE else INVISIBLE
            it.text = if (selectSkill.enable == 1) "切换为全程释放"
            else if (selectSkill.enable == 2) "切换为仅Boss+精英释放"
            else if (selectSkill.enable == 3) "切换为仅Boss释放"
            else "切换为仅战斗释放"
            it.setOnClickListener {
                val last = data ?: return@setOnClickListener
                val enable = if (selectSkill.enable == 1) 2
                            else if (selectSkill.enable == 2) 3
                            else if (selectSkill.enable == 3) 4
                            else 1
                data = last.updateItem(key, selectSkill.copy(enable = enable))
                update()
                save()
            }
        }
        findViewById<TextView>(R.id.skillReleaseType).also {
            it.visibility = if (key != C.V_ATK && selectSkill.enable != 0) VISIBLE else INVISIBLE
            it.text = if (selectSkill.releaseType == 1) "切换为好了就放"
            else "切换为随机降频"
            it.setOnClickListener {
                val last = data ?: return@setOnClickListener
                val enable = if (selectSkill.releaseType == 1) 0
                else 1
                data = last.updateItem(key, selectSkill.copy(releaseType = enable))
                update()
                save()
            }
        }


        findViewById<View>(R.id.skillUserCdUp).visibility = if (selectSkill.enable != 0 && key !=  C.V_ATK) VISIBLE else INVISIBLE
        findViewById<View>(R.id.skillUserCdDown).visibility = if (selectSkill.enable != 0 && key !=  C.V_ATK) VISIBLE else INVISIBLE
        findViewById<View>(R.id.skillUserCdUp).setOnClickListener {
            val last = data ?: return@setOnClickListener
            val cd = if (selectSkill.userCd >= 20000) selectSkill.userCd + 1000
                else if (selectSkill.userCd >= 10000) selectSkill.userCd + 500
                else if (selectSkill.userCd >= 5000) selectSkill.userCd + 200
                else selectSkill.userCd + 100

            data = last.updateItem(key, selectSkill.copy(userCd = cd))
            update()
            save()
        }
        findViewById<View>(R.id.skillUserCdDown).setOnClickListener {
            val last = data ?: return@setOnClickListener

            val cd = if (selectSkill.userCd >= 20000) max(100, selectSkill.userCd - 1000)
            else if (selectSkill.userCd >= 10000) max(100, selectSkill.userCd - 500)
            else if (selectSkill.userCd >= 5000) max(100, selectSkill.userCd - 200)
            else max(100, selectSkill.userCd - 100)

            data = last.updateItem(key, selectSkill.copy(userCd = cd))
            update()
            save()
        }


//        findViewById<View>(R.id.skillUserLongPressTsDown).visibility = if (selectSkill.enable != 0 ) View.VISIBLE else View.INVISIBLE
//        findViewById<View>(R.id.skillUserLongPressTsUp).visibility = if (selectSkill.enable != 0 ) View.VISIBLE else View.INVISIBLE
//        findViewById<View>(R.id.skillUserLongPressTsDown).setOnClickListener {
//            val last = data ?: return@setOnClickListener
//            val ts = max(0, selectSkill.userLongPressTs - 100)
//            data = last.updateItem(key, selectSkill.copy(userLongPressTs = ts))
//            toast("新版本可能不支持技能长按释放了.")
//            update()
//            save()
//        }
//        findViewById<View>(R.id.skillUserLongPressTsUp).setOnClickListener {
//            val last = data ?: return@setOnClickListener
//            val ts = min(10000, selectSkill.userLongPressTs + 100)
//            data = last.updateItem(key, selectSkill.copy(userLongPressTs = ts))
//            toast("新版本可能不支持技能长按释放了.")
//            update()
//            save()
//        }
    }

    private fun save() {
        data?.also { Properties.setGabPanelPlan(it) }
    }
}